﻿using IceCS.GoodsSale.BLL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IceCS.GoodsSale.Winform
{
    /// <summary>
    /// 应用核心类，所有的页面都挂载在此对象上，方便统一管理调用
    /// </summary>
    public class AppCore
    {
        /// <summary>
        /// 
        /// </summary>
        public static string InitPath;
        /// <summary>
        /// 
        /// </summary>
        public static ConnectionInfo ConnectionInfo;

        /// <summary>
        /// 
        /// </summary>
        public static FrmLogin FrmLogin;
        /// <summary>
        /// 注册用户界面 
        /// </summary>
        public static FrmAddUser FrmAddUser;
        /// <summary>
        /// 
        /// </summary>
        public static FrmGetPassword FrmGetPassword;
        /// <summary>
        /// 
        /// </summary>
        public static FrmGoodsSale FrmGoodsSale;
        /// <summary>
        /// 
        /// </summary>
        public static FrmMain FrmMain;
        /// <summary>
        /// 关于界面
        /// </summary>
        public static FrmAbout FrmAbout;
        /// <summary>
        /// 
        /// </summary>
        public static FrmBackupData FrmBackupData;
        /// <summary>
        /// 
        /// </summary>
        public static FrmGoodsPurchase FrmGoodsPurchase;
        /// <summary>
        /// 
        /// </summary>
        public static FrmGoodsQuery FrmGoodsQuery;
        /// <summary>
        /// 
        /// </summary>
        public static FrmHelp FrmHelp;
        /// <summary>
        /// 
        /// </summary>
        public static FrmInventoryAlarm FrmInventoryAlarm;
        /// <summary>
        /// 
        /// </summary>
        public static FrmInventoryQuery FrmInventoryQuery;
        /// <summary>
        /// 
        /// </summary>
        public static FrmPswProInfo FrmPswProInfo;
        /// <summary>
        /// 
        /// </summary>
        public static FrmRestoreData FrmRestoreData;
        /// <summary>
        /// 
        /// </summary>
        public static FrmSaleDataCount FrmSaleDataCount;
        /// <summary>
        /// 
        /// </summary>
        public static FrmSaleDataQuery FrmSaleDataQuery;
        /// <summary>
        /// 
        /// </summary>
        public static FrmUserManage FrmUserManage;
        static AppCore()
        {
            InitPath = Path.Combine(Application.StartupPath, "init.ini");
            FrmLogin = new FrmLogin();
            FrmAddUser = new FrmAddUser();
            FrmGetPassword = new FrmGetPassword();
            FrmGoodsSale = new FrmGoodsSale();
            FrmAbout = new FrmAbout();
            FrmBackupData = new FrmBackupData();
            FrmGoodsPurchase = new FrmGoodsPurchase();
            FrmGoodsQuery = new FrmGoodsQuery();
            FrmHelp = new FrmHelp();
            FrmInventoryAlarm = new FrmInventoryAlarm();
            FrmInventoryQuery = new FrmInventoryQuery();
            FrmPswProInfo = new FrmPswProInfo();
            FrmRestoreData = new FrmRestoreData();
            FrmSaleDataCount = new FrmSaleDataCount();
            FrmSaleDataQuery = new FrmSaleDataQuery();
            FrmUserManage = new FrmUserManage();
            FrmMain = new FrmMain();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool IsInit()
        {
            return File.Exists(InitPath);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ConnectionInfo GetConnectionInfo()
        {
            if (!IsInit())
            {
                throw new Exception("app is not init ");
            }
            var text = File.ReadAllText(InitPath);
            var textArr = text.Split("\r\n");
            var authType = Enum.Parse<AuthType>(textArr[0]);
            var connStr = textArr[1];
            return new ConnectionInfo()
            {
                AuthType = authType,
                ConnectionString = connStr
            };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static void SetConnectionInfo(ConnectionInfo info)
        {
            var text1 = info.AuthType.ToString();
            var text2 = info.ConnectionString;
            var text = text1 + "\r\n" + text2;
            File.WriteAllText(InitPath, text);
        }

        public static string GetWinConnString(string serverName, string dbName)
        {
            string connStr = "Data Source=" + serverName + ";Initial Catalog=" + dbName + ";Integrated Security=True";
            return connStr;
        }

        public static string GetSqlConnString(string serverName, string databaseName, string userName, string password)
        {
            string connStr = "Data Source=" + serverName + ";Initial Catalog=" + databaseName + ";uid=" + userName + ";pwd=" + password;
            return connStr;
        }

        public static string GetDefaultConnString()
        {
            return @"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=DB_GoodsSale;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        }


    }
}
